<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:11
 */
class Base extends MY_Admin_Controller
{
    public function index()
    {
        $limit  = 20;
    	$page   = intval($this->input->get_post('page'));
    	$page   = $page ? $page : 1;
        $offset = ($page - 1) * $limit;
        $uid    = (int)$this->input->get_post('uid');
        $mobile = $this->input->get_post('mobile');
        $type   = $this->input->get_post('type');
        $nickname    = trim($this->input->get_post('nickname'));
        $status      = $this->input->get_post('status');
        $source      = $this->input->get_post('source');
        $start_time  = trim($this->input->get_post('start_time'));
        $end_time    = trim($this->input->get_post('end_time'));
        $recommend_uid  = $this->input->get_post('recommend_uid');
        $recommend_type = $this->input->get_post('recommend_type');


        $where = array();
    	$uid && $where['uid'] = $uid;
    	$mobile && $where['mobile'] = $mobile;
    	!empty($nickname) && $where['nickname like \'%'.$nickname.'%\''] = null;
        ($type == '0' || $type) && $where['type'] = $type;
        ($recommend_uid) && $where['recommend_uid'] = $recommend_uid;
        ($recommend_type == '0' || $recommend_type) && $where['recommend_type'] = $recommend_type;
        ($status == '0' || $status) && $where['status'] = $status;
        ($source == '0' || $source) && $where['source'] = $source;
        (!empty($start_time)) && $where['create_time >'] = date('Y-m-d H:i:s', strtotime($start_time));
        (!empty($start_time)) && $where['create_time <'] = date('Y-m-d H:i:s', strtotime($end_time));


    	if($this->input->get_post('recommend_uid'))
    	{
    		$where['recommend_uid'] = intval($this->input->get_post('recommend_uid'));
    	}

        $this->load->model('user/Model_user','user');
        $total_rows = $this->user->count($where);

        
        //分页
        $this->load->library('pagination');
         
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $limit;
        $config['base_url'] = '/user/Base/index';
         
        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();
        

        $list = $this->user->lists($offset,  $config['per_page'], $where);


        $uids = array_column($list, 'uid');
        $this->load->model('finance/Model_finance_user_capital_account', 'fuca');
        $user_capital_account = $this->fuca->getByIn('uid', $uids, array(), 'uid');


        foreach ($list as &$v)
        {
            $v['balance'] = isset($user_capital_account[$v['uid']]['balance']) ? $user_capital_account[$v['uid']]['balance'] : 0;
        }


        $this->load->model('finance/Model_finance_user_capital_account', 'fuca');
        $total_balance = $this->fuca->sum();


        $recommend_uids = array_column($list, 'recommend_uid');
        $recommend_user = $this->user->getByIn('uid', $recommend_uids, array(), 'uid');

        $data = array
        (
            'infoAll'    => $list,
        	'show_page'  => $show_page,
        	'total_rows' => $config['total_rows'],
            'uid'        => $uid,
        	'mobile'     => $mobile,
            'type'       => $type,
            'status'     => $status,
            'source'     => $source,
            'nickname'   => $nickname,
        	'recommend_uid'  => $recommend_uid,
            'recommend_type' => $recommend_type,
            'start_time'     => $start_time,
            'end_time'       => $end_time,
            'total_balance'  => $total_balance,
            'recommend_user' => $recommend_user,
        );
        
        $this->load->view('admin/user/base/index',$data);
    }


    /*
     * user 添加,编辑
     * */
    public function create()
    {
        $uid = (int)$this->uri->segment(4);

        //用户等级
        $this->load->model('user/Model_user_level_config', 'level');
        $levelInfo = $this->level->lists('0','100000',array(),true,'lid asc');

        //用户信息
        $this->load->model('user/Model_user', 'user');
        $userInfo  = $this->user->getById($uid);

        //用户详情
        $this->load->model('user/Model_user_info', 'info');
        $userDetail  = $this->info->getOne(array('uid'=>$uid));


        /*
        //推荐人列表
        $this->load->model('user/Model_user','user');
        $list = $this->user->lists('0','100',array(),true,'uid desc');


        //uid为key uname为val
        $recommendInfo = array();
        foreach($list as $v)
        {
            $recommendInfo[$v['uid']] = $v['uname'];
        }
        //*/


        $this->load->model('common/Model_file','file');
        $fileInfo = $this->file->getById($userInfo['header']);


        $data = array
        (
            'userInfo'       => $userInfo,
            'levelInfo'      => $levelInfo,
            'userDetail'     => $userDetail,
            //'recommendInfo'  => $recommendInfo,
            'fileInfo'       => $fileInfo,
        );

        $this->load->view('admin/user/base/create', $data);

    }

    /*
     * user资料编辑后保存
     *
     * */
    public function save()
    {
        $uid = (int)$this->input->get_post('uid');
        $ui_id = (int)$this->input->get_post('ui_id');
       
        $data = array(
            'lid'           =>      (int)$this->input->get_post('lid'),
            'nickname'      =>      trim($this->input->get_post('nick')),
            'mobile'        =>      $this->input->get_post('mobile'),
            'status'        =>      (int)$this->input->get_post('status'),
            'source'        =>      (int)$this->input->get_post('source'),
            //判断是否选择图片如果选择了图片则获取新突破地址或者判断是新建页面还是修改页面
            'header'        =>     !empty($_FILES['up_file']['tmp_name'])?$this->uploadFile('up_file'):(isset($uid)?$this->input->get_post('pic'):''),
            'sex'           =>      (int)$this->input->get_post('sex'),
        );

        $data_info = array(
            'occupation'    =>      trim($this->input->get_post('occupation')),
            'introduction'  =>      trim($this->input->get_post('introduction')),
            'to_focus_num'  =>      (int)$this->input->get_post('to_focus_num'),
            'add_data'      =>      trim($this->input->get_post('add_data')),
            'focus_num'     =>      (int)$this->input->get_post('focus_num'),
            'like'          =>      (int)$this->input->get_post('like'),
        );

        //更新user表
        $this->load->model('user/Model_user', 'user');
        $this->user->save($data, $uid);

        //更新user_info表
        $this->load->model('user/Model_user_info', 'userinfo');
        $this->userinfo->save($data_info, $ui_id);

        $this->load->helper('url');
        redirect(url('admin').'user/base/index');
    }

    /**
     * 删除用户
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $uid = (int)$this->input->get_post('uid');

            if (!$uid)
            {
                $response = error(10002);
                break;
            }

            $this->load->model('user/model_user', 'user');

            $this->load->model('user/model_user_info', 'userInfo');
            $item = $this->userInfo->getOne(array('uid' => $uid));

            if (empty ($item))
            {
                $response = error(10002);
                break;
            }

            $res_user = $this->user->delete(array('uid' => $uid));
            $res_info = $this->userInfo->delete(array('uid' => $uid));

            if (!$res_user || !$res_info)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
    
    public function delete_pay_password()
    {
    	$uid = (int)$this->input->get_post('uid');
    
    	$response = array('code' => '0', 'msg' => '操作成功');
    	
    	$this->load->model('finance/Model_finance_pay_password', 'fpp');
    	$this->fpp->delete(array('uid'=>$uid));
    
    	$this->json_output($response);
    }


    /**
     * 拉黑
     */
    public function pull_black()
    {
        $response = array('code' => '200', 'msg' => '操作成功');


        do {
            $uid    = (int)$this->input->get_post('uid');
            $status = (int)$this->input->get_post('status');

            if (!$uid)
            {
                $response = error(10002);
                break;
            }


            if (!array_key_exists($status, config_item('user_status')))
            {
                $response = error(10002);
                break;
            }


            $this->load->model('user/model_user', 'u');
            $item = $this->u->getById($uid);
            if (empty ($item))
            {
                $response = error(10002);
                break;
            }


            $save_rs = $this->u->save(array('status' => $status), $uid);
            if (!$save_rs)
            {
                $response = error(10126);
                break;
            }

        }while (false);


        $this->json_output($response);
    }


    public function delete_video()
    {
        $response = array('code' => '200', 'msg' => '操作成功');


        do {
            $uid    = (int)$this->input->get_post('uid');

            if (!$uid)
            {
                $response = error(10002);
                break;
            }


            $this->load->model('user/model_user', 'u');
            $item = $this->u->getById($uid);
            if (empty ($item))
            {
                $response = error(10002);
                break;
            }


            $this->load->model('user/model_user_info', 'ui');
            $save_rs = $this->ui->update(array('video' => 0), array('uid' => $uid));
            if (!$save_rs)
            {
                $response = error(10126);
                break;
            }

        }while (false);


        $this->json_output($response);
    }
}